﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FinPlusInterfaces;

namespace FinPlusAssembler
{
    public static class PodHelpers
    {
        public static object[] ToParam(this IPod pod,  IList<string> exclude = null)
        {
            var objs = new List<object>();
            foreach (var bean in pod.OrderBy(b=> b.Index).ToList())
            {
                if (bean.Name.EnumIsDefined<PodField>() || bean.Name.Equals("Dock") || (exclude != null && exclude.Contains(bean.Name)))
                    continue;

                objs.Add(bean.Object);
            }

            return objs.ToArray();
        }

        public static object Property(this IPod pod, string name)
        {
            try
            {
                if (!HasProperty(pod, name))
                    return null;

                var obj = pod.Object.GetType().GetProperty(name).GetValue(pod.Object, null);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public static bool HasProperty(this IPod pod, string name)
        {
            if (pod == null || pod.Object == null)
                return false;

            var type = pod.Object.GetType();
            return type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) != null;
        }
    }
}
